package com.ruoyi.gams.device.led;

import com.ruoyi.gams.domain.GamsDeviceBussinessRelation;
import com.ruoyi.gams.domain.GamsWindow;
import com.ruoyi.gams.domain.GamsWindowScreen;
import com.ruoyi.gams.domain.GamsWindowText;
import com.ruoyi.gams.mapper.GamsDeviceBussinessRelationMapper;
import com.ruoyi.gams.mapper.GamsWindowScreenMapper;
import com.ruoyi.gams.mapper.GamsWindowTextMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


import java.util.List;

import static com.ruoyi.gams.device.led.Protocol.ledsender;
import static com.ruoyi.gams.device.led.Protocol.send_data;


@Component
@Slf4j
public class ScreenService
{

    @Autowired
    private GamsWindowTextMapper windowTextMapper;

    @Autowired
    private GamsWindowScreenMapper windowScreenMapper;

    @Autowired
    private GamsDeviceBussinessRelationMapper relationMapper;

    @Async
    public void refreshWindowScreen(Integer windowScreenId, int roottype)
    {
        GamsWindowScreen screen = windowScreenMapper.selectGamsWindowScreenById(windowScreenId);
        if (screen == null)
        {
            log.info("未找到控制卡信息：" + windowScreenId);
            return;
        }
        refresh(screen, roottype);
    }

    /**
     * 刷新窗口屏
     *
     * @param window
     */
    @Async
    public void refreshWindowScreen(GamsWindow window, int roottype)
    {
        if (window == null)
        {
            log.info("窗口信息不能为空");
            return;
        }
        Integer windowScreenId = window.getWindowScreenId();
        GamsWindowScreen screen = windowScreenMapper.selectGamsWindowScreenById(windowScreenId);
        if (screen == null)
        {
            log.info("未找到控制卡信息：" + windowScreenId);
            return;
        }
        refresh(screen, roottype);
    }

    public void refresh(GamsWindowScreen screen, int roottype)
    {
        log.info("更新控制卡内容：" + screen.getScreenName());
        //生成节目数据
        ledsender.MakeRoot(roottype, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(screen.getLeft(), screen.getTop(), screen.getWidth(), screen.getHeight());
        ledsender.AddLeaf(0, 20000);
        List<GamsWindowText> list = windowTextMapper.findByWindowScreenId(screen.getId());
        for (GamsWindowText e : list)
        {
            ledsender.AddText(
                    e.getLeft(),
                    e.getTop(),
                    e.getWidth(),
                    e.getHeight(),
                    e.getText(),
                    e.getFontName(),
                    e.getFontSize(),
                    Protocol.getColor(e.getFontBackColor()),
                    Protocol.getColor(e.getFontColor()),
                    e.getFontStyle(),
                    e.getHalign(),
                    false,
                    e.getInmethod(),
                    e.getInspeed(),
                    e.getOutmethod(),
                    e.getOutspeed(),
                    e.getStopmethod(),
                    e.getStopspeed(),
                    e.getStoptime());
        }
        send_data(screen.getLedHost(), screen.getLedPort());
    }
}
